﻿using System;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using SysComm;
using SysOperationLayer.DBHandle;
using IPMS.Common;
using Resources;

public partial class OptProjectClass : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        string strCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();

        if (Session["Culture"] != null)
        {
            strCulture = Session["Culture"].ToString();
        }

        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(strCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(strCulture);
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!SysUtil.IsAdministratorOrNormalUserType())
        {
            Response.Redirect("ErrorPage/NotAllowed.aspx");
        }
        else if (!IsPostBack)
        {
            ((Label)Master.FindControl("lblMstTitle")).Text = " | " + ISitemap.Settings + " | " + ISitemap.MaintenanceFiles.ToString() + " | " + ISitemap.ProjectClass;
            panelDeleted.Visible = SysUtil.IsAdministratorUsertype();
            GetProjectClassList("", "");
        }
    }

    private void GetProjectClassDeleteList(string projectClass, string description)
    {
        ProjectClassDB objDB = new ProjectClassDB();
        gvDeleted.DataSource = objDB.getProjectClassList(projectClass, description, 1);
        gvDeleted.DataBind();
        objDB.CloseDB();
    }

    private void GetProjectClassActiveList(string projectClass, string description)
    {
        ProjectClassDB objDB = new ProjectClassDB();
        gvActive.DataSource = objDB.getProjectClassList(projectClass, description, 0); ;
        gvActive.DataBind();
        objDB.CloseDB();
    }

    private void GetProjectClassList(string projectClass, string description)
    {
        GetProjectClassActiveList(projectClass, description);

        if (SysUtil.IsAdministratorUsertype())
        {
            GetProjectClassDeleteList(projectClass, description);
        }
    }

    protected void OnClick(object sender, EventArgs e)
    {
        if (sender == btnSearch)
        {
            GetProjectClassList(txtSearchProjectClass.Text.Trim(), txtSearchDescription.Text.Trim());
        }
        else if (sender == btnNew)
        {
            Response.Redirect("OptProjectClassAdd.aspx");
        }
    }

    protected void gvActive_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvActive.PageIndex = e.NewPageIndex;
        GetProjectClassActiveList(txtSearchProjectClass.Text.Trim(), txtSearchDescription.Text.Trim());
    }

    protected void gvDeleted_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvDeleted.PageIndex = e.NewPageIndex;
        GetProjectClassDeleteList(txtSearchProjectClass.Text.Trim(), txtSearchDescription.Text.Trim());
    }

    protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandSource is ImageButton)
        {
            GridViewRow curRow = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
            GridView gv = (GridView)sender;
            ProjectClassDB objDB = new ProjectClassDB();
            string strProjectClass = gv.DataKeys[curRow.RowIndex].Values["ProjectClass"].ToString();

            if (e.CommandName == "halfDelete")
            {
                if (objDB.Delete(strProjectClass,
                                    Page.User.Identity.Name.ToString(),
                                    HttpContext.Current.Request.UserHostAddress) == 0)
                {
                    new LogDB(objDB.GetDB()).writeSucLog(SysUtil.m_functiong_projectclass, "Delete Project Class Success! Project Class Code: " + strProjectClass);
                    Jscript.Alert("Delete Project Class Success!", Page);
                    GetProjectClassList(txtSearchProjectClass.Text.Trim(), txtSearchDescription.Text.Trim());
                }
                else
                {
                    new LogDB(objDB.GetDB()).writeFaileLog(SysUtil.m_functiong_projectclass, "Delete Project Class Failure! Project Class Code: " + strProjectClass);
                    Jscript.Alert("Delete Project Class Failure!", Page);
                }

                objDB.CloseDB();
            }
            else if (e.CommandName == "Restore")
            {
                // do restore
                if (objDB.Restore(strProjectClass) == 0)
                {
                    new LogDB(objDB.GetDB()).writeSucLog(SysUtil.m_functiong_projectclass, "Restore Project Class Success! Project Class Code: " + strProjectClass);
                    Jscript.Alert("Restore Project Class Success!", Page);
                    GetProjectClassList(txtSearchProjectClass.Text.Trim(), txtSearchDescription.Text.Trim());
                }
                else
                {
                    new LogDB(objDB.GetDB()).writeFaileLog(SysUtil.m_functiong_projectclass, "Restore Project Class Failure! Project Class Code: " + strProjectClass);
                    Jscript.Alert("Restore Project Class Failure!", Page);
                }
            }
            else if (e.CommandName == "ForceDelete")
            {
                if (objDB.Trash(strProjectClass) == 0)
                {
                    new LogDB(objDB.GetDB()).writeSucLog(SysUtil.m_functiong_projectclass, "Trash Project Class Success! Project Code: " + strProjectClass);
                    Jscript.Alert("Trash Project Class Success!", Page);
                    GetProjectClassList(txtSearchProjectClass.Text.Trim(), txtSearchDescription.Text.Trim());
                }
                else
                {
                    new LogDB(objDB.GetDB()).writeFaileLog(SysUtil.m_functiong_projectclass, "Trash Project Class Failure! Project Class Code: " + strProjectClass);
                    Jscript.Alert("Trash Project Class Failure!", Page);
                }
            }

            objDB.CloseDB();
        }
    }
}
